﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class product_saving_admin_saving_applicant : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SavingApplicantBLL app_dal = new SavingApplicantBLL();
        DataSet applicant_list = app_dal.getAllList();
        gvApplicants.DataSource = applicant_list;
        gvApplicants.DataBind();
        //string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        //SqlConnection myConnection = new SqlConnection(strConnectionString);

        //SqlCommand myCommand = new SqlCommand("SELECT * FROM SAVING_ACCOUNT", myConnection);
        ////Open Connection
        //myConnection.Open();
        ////Execute Command using a DATAREADER
        //SqlDataReader myReader = myCommand.ExecuteReader();
        ////Retrieving Data from DataReader
        //gvApplicants.DataSource = myReader;
        ////Bind the control to the datasource
        //gvApplicants.DataBind();

        ////Close the data reader
        //myReader.Close();
        ////Close the connection
        //myConnection.Close();
    }
    protected void gvApplicants_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = gvApplicants.SelectedRow;


        Response.Redirect("~/product/saving/admin/saving_details.aspx?custID=" + row.Cells[2].Text);

    }

    protected void ButtonNumber1_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = DisplayTextBox1.Text + "1";
    }
    protected void ButtonNumber2_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = DisplayTextBox1.Text + "2";
    }
    protected void ButtonNumber3_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = DisplayTextBox1.Text + "3";
    }
    protected void ButtonNumber4_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = DisplayTextBox1.Text + "4";
    }
    protected void ButtonNumber5_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = DisplayTextBox1.Text + "5";
    }
    protected void ButtonNumber6_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = DisplayTextBox1.Text + "6";
    }
    protected void ButtonNumber7_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = DisplayTextBox1.Text + "7";
    }
    protected void ButtonNumber8_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = DisplayTextBox1.Text + "8";
    }
    protected void ButtonNumber9_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = DisplayTextBox1.Text + "9";
    }
    protected void ButtonNumber0_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = DisplayTextBox1.Text + "0";
    }
    protected void ButtonNumberPlus_Click(object sender, EventArgs e)
    {

        DisplayTextBox1.Text = DisplayTextBox1.Text + "+";
    }


    protected void ButtonNumberMinus_Click(object sender, EventArgs e)
    {

        DisplayTextBox1.Text = DisplayTextBox1.Text + "-";

    }


    protected void ButtonNumberMulti_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = DisplayTextBox1.Text + "*";

    }


    protected void ButtonNumberDevide_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = DisplayTextBox1.Text + "/";
    }



    protected void ButtonNumberdot_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = DisplayTextBox1.Text + ".";
    }

    protected void Button21_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = DisplayTextBox1.Text + "(";
    }

    protected void Button22_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = DisplayTextBox1.Text + ")";
    }

    //Event for Button AC
    protected void Button25_Click(object sender, EventArgs e)
    {
        DisplayTextBox1.Text = "";
        DisplayLabel.Text = "";
    }

    //Event for Button DEL
    protected void Button24_Click(object sender, EventArgs e)
    {
        try
        {
            string DisplayText = DisplayTextBox1.Text;

            int LastIndex = DisplayText.Length;

            DisplayTextBox1.Text = DisplayTextBox1.Text.Remove(LastIndex - 1);
        }

        catch (Exception ex)
        {

        }

    }

    protected void ButtonNumberEqual_Click(object sender, EventArgs e)
    {

        try
        {

            string Input = DisplayTextBox1.Text;

            DataTable table = new DataTable();

            Object answer;

            answer = table.Compute(Input, null);

            DisplayLabel.Text = answer.ToString();
        }

        catch (Exception ex)
        {
            DisplayLabel.Text = "ERROR";
        }

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        decimal amount = decimal.Parse(tbAmount.Text);
        string strType = "INTEREST RATE";
        string strTo = tbTo.Text;
        string strStatus = "SUCCESS";

       

        string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection myConnection = new SqlConnection(strConnectionString);
       
        try
        {

            myConnection = DBManager.getSqlConnection();
            SqlCommand myCommandp = new SqlCommand("update [SAVING_ACCOUNT] SET [balance] = balance+" + amount + " WHERE [accountNo] = " + tbTo.Text, myConnection);
            myConnection.Open();
            myCommandp.Parameters.AddWithValue("@balance", amount);
            myCommandp.Parameters.AddWithValue("@account", tbTo.Text);
            myCommandp.ExecuteNonQuery();



        myConnection = DBManager.getSqlConnection();
        SqlCommand myCommandd = new SqlCommand("INSERT INTO SUMMARY(transactionDate, transactionType, transactionStatus, accountNo,transactionAmount)" + "VALUES(@date, @type, @status, @accNo, @transactionAmount)", myConnection);
        myConnection.Open();
        myCommandd.Parameters.AddWithValue("@date", DateTime.Now);
        myCommandd.Parameters.AddWithValue("@type", strType);
        myCommandd.Parameters.AddWithValue("@status", strStatus);
        myCommandd.Parameters.AddWithValue("@accNo", tbTo.Text);
        myCommandd.Parameters.AddWithValue("@transactionAmount", amount);
        myCommandd.ExecuteNonQuery();

        //myConnection = DBManager.getSqlConnection();
        //SqlCommand myCommandp = new SqlCommand("update [SAVING_ACCOUNT] SET [balance] = balance+" + amount + " WHERE [accountNo] = " + tbTo.Text, myConnection);
        //myConnection.Open();
        //myCommandp.Parameters.AddWithValue("@balance", amount);
        //myCommandp.Parameters.AddWithValue("@account", tbTo.Text);
        //myCommandp.ExecuteNonQuery();

        
        Label1.Text ="Interest Rate Success, please log in to check for the updated balance. Thank you.";

        myConnection.Close();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
        Response.Write(ex);
        }
        finally
        {
        myConnection.Close();
        }
        }
    
}